<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Lottrery extends Admin_Controller {
	
	function __construct(){
		parent::__construct();
		$this->admin_navigation->parent_active('lottrery');
	}
	
	function lottrery_manager(){
		$this->load->model('lottrery/lottrery_model');
	    $columns = array(
						array(
							'name' => 'ID',
							'type' => 'id',
							'index'=>'id',
							'sort_column'=>true
							),
						array(
							'name'=>$this->__('Date'),
							'type'=>'text',
							'filter'=>'date',
							'index'=>'date',
							'sort_column'=>true
						),
						array(
							'name'=>$this->__('City'),
							'type'=>'text',
							'filter'=>'CityName',
							'index'=>'CityName',
							'sort_column'=>true
						),
						array(
							'name'=>$this->__('DB'),
							'type'=>'text',
							'filter'=>'db',
							'index'=>'db',
							'sort_column'=>true
						),
						array(
							'name'=>$this->__('G1'),
							'type'=>'text',
							'filter'=>'G1',
							'index'=>'G1',
							'sort_column'=>true
						),
						array(
							'name'=>$this->__('G2'),
							'type'=>'text',
							'filter'=>'G2',
							'index'=>'G2',
							'sort_column'=>true
						),
						array(
							'name'=>$this->__('G3'),
							'type'=>'text',
							'filter'=>'G3',
							'index'=>'G3',
							'sort_column'=>true
						),
//						array(
//							'name'=>$this->__('G4'),
//							'type'=>'text',
//							'filter'=>'G4',
//							'index'=>'G4',
//							'sort_column'=>true
//						),
//						array(
//							'name'=>$this->__('G5'),
//							'type'=>'text',
//							'filter'=>'G5',
//							'index'=>'G5',
//							'sort_column'=>true
//						),
//						array(
//							'name'=>$this->__('G6'),
//							'type'=>'text',
//							'filter'=>'G6',
//							'index'=>'G6',
//							'sort_column'=>true
//						),
//						array(
//							'name'=>$this->__('G7'),
//							'type'=>'text',
//							'filter'=>'G7',
//							'index'=>'G7',
//							'sort_column'=>true
//						),
//						array(
//							'name'=>$this->__('G8'),
//							'type'=>'text',
//							'filter'=>'G8',
//							'index'=>'G8',
//							'sort_column'=>true
//						),
						
		);
		$this->grid->columns($columns);
		$this->grid->datasource('lottrery/lottrery_model','getAll');
		$this->grid->set_title($this->__('Lottrery Manager'));
		$this->grid->add_button($this->__('Add New'),admin_url('*/lottrery/add_lottrery'),'add');
		$this->grid->set_row_action('edit_lottrery');

		$this->grid->total_rows($this->lottrery_model->get_count());
		$this->grid->add_mass_action('delete', array(
			'label'=>$this->__('Delete'),
			'url'      => admin_url('*/lottrery/delete_lottrery'),
            'confirm'  =>$this->__('Are you sure ?')
		));
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_lottrery(){
		$lottrery_id = $this->input->get_param('id',false);
		$this->load->model('lottrery/lottrery_model');
		$this->load->model('lottrery/lottrery_city_model');
		if ($lottrery_id) {
			$data = $this->lottrery_model->get_lottrery(array('id'=>$lottrery_id));
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'lottrery_form',
			'action'=>admin_url('*/lottrery/edit_post_lottrery',array('id'=>$lottrery_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array(
			'legend'=>'Ganeral'
		));
		
		$this->form->add_filed('Date','date',array(
			'label'=>$this->__('Date'),
			'name'=>'Date',
			'required'=>true
		));
		$this->form->add_filed('city_id','select',array(
			'label'=>$this->__('Game Type'),
			'name'=>'city_id',
			'values'=>$this->lottrery_city_model->lottrery_options()
		));
		$this->form->add_filed('db','text',array(
			'label'=>$this->__('DB'),
			'name'=>'DB',
			'required'=>true
		));
		$this->form->add_filed('g1','text',array(
			'label'=>$this->__('G1'),
			'name'=>'G1',
			'required'=>true
		));
		$this->form->add_filed('g2','text',array(
			'label'=>$this->__('G2'),
			'name'=>'G2',
			'required'=>true
		));
		$this->form->add_filed('g3','text',array(
			'label'=>$this->__('G3'),
			'name'=>'G3',
			'required'=>true
		));
		$this->form->add_filed('g4','text',array(
			'label'=>$this->__('G4'),
			'name'=>'G4',
			'required'=>true
		));
		$this->form->add_filed('g5','text',array(
			'label'=>$this->__('G5'),
			'name'=>'G5',
			'required'=>true
		));
		$this->form->add_filed('g6','text',array(
			'label'=>$this->__('G6'),
			'name'=>'G6',
			'required'=>true
		));
		$this->form->add_filed('g7','text',array(
			'label'=>$this->__('G7'),
			'name'=>'G7',
			'required'=>true
		));
		$this->form->add_filed('g8','text',array(
			'label'=>$this->__('G8'),
			'name'=>'G8',
		));
		
		
		
		$this->load->view('admin/form',array('form'=>$this->form->to_html()));
	}
	
	function add_lottrery(){
		$this->_forward('edit_lottrery');
	}
	
	function edit_post_lottrery(){
		$lottrery_id = $this->input->get_param('id',false);
		$this->load->model('lottrery/lottrery_model');
		if (($data = $this->input->post())){
			if ($lottrery_id){
				$this->lottrery_model->update_lottrery($data,$lottrery_id);
			}else{
				$this->lottrery_model->insert_lottrery($data);
			}
		}
		$this->_redirect(admin_url('*/lottrery/lottrery_manager'));
	}
	
	function delete_lottrery(){
		$cids = explode(',',$this->input->get_param('id',false));
		$this->load->model('lottrery/lottrery_model');
		foreach ($cids as $cid){
			$this->lottrery_model->delete_lottrery($cid);
		}
		$this->_redirect(admin_url('*/lottrery/lottrery_manager'));
	}
}
